import pika
import time
#receive
connection = pika.BlockingConnection(
    pika.ConnectionParameters(host='localhost'))
channel = connection.channel()

channel.queue_declare(queue='task_queue', durable=True)#设置队列为持久
print(' [*] Waiting for messages. To exit press CTRL+C')


def callback(ch, method, properties, body):
    print(" [x] Received %r" % body.decode())
    time.sleep(body.count(b'.'))#每个点伪造一秒钟
    print(" [x] Done")
    ch.basic_ack(delivery_tag=method.delivery_tag)


channel.basic_qos(prefetch_count=1)#使用prefetch_count=1设置channel#basic_qos通道方法，使用basic.qos协议方法告诉RabbitMQ一次不要给一个工人多个消息，就是合理的分配消息
channel.basic_consume(queue='task_queue', on_message_callback=callback)

channel.start_consuming()